package com.nand.module.function.adapter;

import android.animation.Animator;
import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.SpannableString;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.animation.ScaleInAnimation;
import com.nand.module.function.R;
import com.nand.module.function.bean.ClampingBean;
import com.nand.module.function.bean.MMMBean;

import java.util.ArrayList;
import java.util.List;

/**
 * created by 86185
 * on 2020/2/7
 */
public class ClampingAdapter extends RecyclerView.Adapter<ClampingAdapter.ViewHolder> implements View.OnClickListener {
    private Context mContext;
    private String TAG = "ClampingAdapter";
    private int type;
    private List<ClampingBean> mList = new ArrayList<>();
    private ScaleInAnimation mSelectAnimation = new ScaleInAnimation();
    private ClampingAdapter.OnItemClickListener mOnItemClickListener = null;

    public ClampingAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setData(List<ClampingBean> data) {
        this.mList = data;
        notifyDataSetChanged();
    }


    @Override
    public ClampingAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View layout = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_clamping, viewGroup, false);
        layout.setOnClickListener(this);
        return new ClampingAdapter.ViewHolder(layout);
    }

    @Override
    public void onBindViewHolder(ClampingAdapter.ViewHolder viewHolder, int position) {
        viewHolder.itemView.setTag(position);
        ClampingBean bean = mList.get(position);
        viewHolder.mTextview1.setText(bean.getFixtureCode());
        viewHolder.mTextview2.setText( Html.fromHtml("工装名称:&#12288;<font color='#696969'>"+bean.getFixtureName()+"</font>"));
        viewHolder.mTextview3.setText( Html.fromHtml("设备归属:&#12288;<font color='#696969'>"+bean.getBelongCode()+"</font>"));
        viewHolder.mTextview4.setText( Html.fromHtml("供&#8194;应&#8194;商:&#12288;<font color='#696969'>"+bean.getSupplierCode()+"</font>"));
        viewHolder.mTextview5.setText( Html.fromHtml("总&#8194;使&#8194;用:&#12288;<font color='#696969'>"+bean.getUsage()+"</font>"));
        viewHolder.mTextview6.setText(Html.fromHtml("已&#8194;维&#8194;修:&#12288;<font color='#696969'>"+bean.getRepairAmount()+"</font>"));
        switch (bean.getFixtureState()) {
            case 0:
                viewHolder.mTextview7.setText( "在库");
                break;
            case 1:
                viewHolder.mTextview7.setText( "安装");
                break;
            case 2:
                viewHolder.mTextview7.setText( "维修");
                break;
            case 3:
                viewHolder.mTextview7.setText("调试");

                break;
        }

    }

    @Override
    public int getItemCount() {
        if (mList != null)
            return mList.size();
        return 0;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView mTextview1, mTextview2, mTextview3, mTextview4, mTextview5,mTextview6,mTextview7;

        public ViewHolder(View itemView) {
            super(itemView);
            mTextview1 = itemView.findViewById(R.id.textview1);
            mTextview2 = itemView.findViewById(R.id.textview2);
            mTextview3 = itemView.findViewById(R.id.textview3);
            mTextview4 = itemView.findViewById(R.id.textview4);
            mTextview5 = itemView.findViewById(R.id.textview5);
            mTextview6 = itemView.findViewById(R.id.textview6);
            mTextview7 = itemView.findViewById(R.id.textview7);


        }

    }
    //两次点击按钮之间的间隔，目前为1000ms
    private static final int MIN_CLICK_DELAY_TIME = 1000;
    private long lastClickTime;
    public static interface OnItemClickListener {

        void onItemClick(View view, int position);

    }
    @Override
    public void onViewAttachedToWindow(@NonNull ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        addAnimation(holder);
    }


    private void addAnimation(ViewHolder holder) {
        for (Animator anim : mSelectAnimation.getAnimators(holder.itemView)) {
            anim.setDuration(300).start();
            anim.setInterpolator(new LinearInterpolator());
        }
    }
    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            long curClickTime = System.currentTimeMillis();
            if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
                lastClickTime = curClickTime;
                //注意这里使用getTag方法获取position
                mOnItemClickListener.onItemClick(v, (int) v.getTag());
            }
        }
    }

    public void setOnItemClickListener(ClampingAdapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
